package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.Dict;
import com.xmy.cultivate.entity.TeacherLeadsYearClass;
import com.xmy.cultivate.service.IDictService;
import com.xmy.cultivate.service.ITeacherLeadsYearClassService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.ConfigData;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 老师带班年级 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-02-25
 */
@RestController
@RequestMapping("/api/teacherLeadsYearClass")
public class TeacherLeadsYearClassController  extends BaseController{

    @Autowired
    ITeacherLeadsYearClassService iTeacherLeadsYearClassService;

    @RequestMapping("/getTeacherLeadsYearClass")
    public Result getTeacherLeadsYearClass(@RequestParam(value = "id", defaultValue = "") String id) {

        if (id == null || id.isEmpty()){
            return new Result(Code.ERROR,"","id不能为空！");
        }
        List<TeacherLeadsYearClass> teacherLeadsYearClassList = iTeacherLeadsYearClassService.getTeacherLeadsYearClass(id);

        return new Result(Code.OK,teacherLeadsYearClassList,Code.GET_OK_MSG);
    }


    @RequestMapping("/getAutoYearClassList")
    public Result getAutoYearClassList(@RequestParam(value = "id", defaultValue = "") String id) {

        if (id == null || id.isEmpty()){
            return new Result(Code.ERROR,"","id不能为空！");
        }
        TeacherLeadsYearClass teacherLeadsYearClass = iTeacherLeadsYearClassService.getAutoYearClassList(id);

        Map<Integer, String> yearClassMap = new HashMap<>();
        if (teacherLeadsYearClass != null && teacherLeadsYearClass.getYearClassIds() != null) {
            String yearClassIds = teacherLeadsYearClass.getYearClassIds();
            String[] split = yearClassIds.split(",");
            for (String s : split) {
                Integer yearClassId = Integer.valueOf(s);
                String yearClassName = ConfigData.getYearClassForNum(yearClassId);
                if (yearClassName != null) {
                    yearClassMap.put(yearClassId, yearClassName);
                }
            }
        }
        Map<String, Object> data = new HashMap<>();
        data.put("yearClassMap", yearClassMap);
//        teacherLeadsYearClass.setData(data);

        return new Result(Code.OK,yearClassMap,Code.GET_OK_MSG);
    }
    @PostMapping("/addTeacherLeadsYearClass")
    public Result addTeacherLeadsYearClass( @RequestBody List<TeacherLeadsYearClass> teacherLeadsYearClassList) {


        if (teacherLeadsYearClassList == null || teacherLeadsYearClassList.isEmpty()) {
            return new Result(Code.ERROR, "", "请求数据不能为空");
        }

        for (TeacherLeadsYearClass teacherLeadsYearClass : teacherLeadsYearClassList) {
            teacherLeadsYearClass.setCreatedId(this.adminId);
        }

        boolean saveAll = iTeacherLeadsYearClassService.saveBatch(teacherLeadsYearClassList);
        if (saveAll) {
            return new Result(Code.OK, "", "添加成功");
        }
        return new Result(Code.ERROR, "", "添加失败");
    }
}
